﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Xml;
using System.IO;
namespace FetionWebMessenger
{
    public partial class FetionSendSomeAction : System.Web.UI.Page
    {
        private String content = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpRequest request = this.Page.Request;
            System.IO.Stream s = Request.InputStream;
            int count = 0;
            byte[] buffer = new byte[1024];
            StringBuilder builder = new StringBuilder();
            while ((count = s.Read(buffer, 0, 1024)) > 0)
            {

                builder.Append(Encoding.UTF8.GetString(buffer, 0, count));

            }
            if (SessionObj == null)
            {
                return;
            }
            HttpResponse response = this.Page.Response;
            response.ContentType = "text/xml";
            response.AppendHeader("Cache-Control", "no-cache");
            List<String> list = ParesXML(builder.ToString());
            foreach (String uri in list)
            {
                SessionObj.SendMsgTo(uri, content);
            }
            String result = "<?xml version='1.0' encoding='UTF-8'?><response>";
            result += "</response>";
            response.Write(result);
        }
        protected FetionCore SessionObj
        {
            get
            {
                return (FetionCore)Session["FetionCore"];
            }
        }
        private List<String> ParesXML(String XML)
        {
            List<String> list = new List<String>();
            string strpath = System.Web.HttpContext.Current.Server.MapPath("temp.xml");
            StreamWriter sw = new StreamWriter(strpath);
            sw.Write(XML);
            sw.Close();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(strpath);
            XmlNode xn = xmlDoc.SelectSingleNode("Contacts");
            XmlNodeList xnl = xn.ChildNodes;
            foreach (XmlNode xnf in xnl)
            {
                XmlElement xe = (XmlElement)xnf;
                String tmp = xe.InnerText;
                list.Add(tmp);
            }
            xn = xmlDoc.SelectSingleNode("content");
            content = xn.InnerText;
            return list;
        }
    }
}
